package btreegui;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;

/**
 *
 * @author Bart
 */
public class DrawTreeElement {

    private static int width = 30;
    private static int height = 30;
    private int x, y = 0;
    private int xOffset = 0;
    private Color backcolor;
    private Color forecolor;
    private Font f;
    private FontMetrics fm;
    private String value;
    int xx;
    int sw;

    public DrawTreeElement(JPanel panel, String value, int nr, Color back, Color fore) {
        this.value = value; //Wert 
        this.x = (nr * width);
        f = new Font("Arial", Font.BOLD, 16);
        fm = panel.getGraphics().getFontMetrics(f);
        backcolor = back;
        forecolor = fore;
        xx = width;
        sw = fm.stringWidth(value); // width of string
        if (sw > xx) {   // draw alternative string near center of applet
            DrawTreeElement.setWidth(sw + 12);
            xx=width;
        }
    }

    public void paintComponent(Graphics2D g2d, int XBucketOffset, int YBuckrtOffset) {
        g2d.setColor(backcolor);
        Rectangle2D rect = new Rectangle2D.Double(x + xOffset + XBucketOffset, y + YBuckrtOffset, width, height);   //gefüllter Hintergrund 
        g2d.fill(rect);
        g2d.draw(rect);
        g2d.setColor(forecolor);
        g2d.drawRect(x + xOffset + XBucketOffset, y + YBuckrtOffset, width, height);      //absolute koordinaten!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        //Value zeichnen
        g2d.setFont(f);
        int yy = YBuckrtOffset + (height) / 2 + 6; // height of rect
        g2d.drawString(value, (xOffset + XBucketOffset + x + (xx - sw) / 2), y + yy);
    }

    public void setBackcolor(Color backcolor) {
        this.backcolor = backcolor;
    }

    public void setForecolor(Color forecolor) {
        this.forecolor = forecolor;
    }

    public Color getBackcolor() {
        return backcolor;
    }

    public Color getForecolor() {
        return forecolor;
    }

    public String getValue() {
        return value;
    }

    public static int getHeight() {
        return height;
    }

    public static int getWidth() {
        return width;
    }

    public static void setWidth(int width) {
        DrawTreeElement.width = width;
    }
}
